<?php
class Blog_model extends CI_Model {

	/**
	 * @author Pwint Phyu Shwe
	 * Blog Model for tbl_blog
	 * Refer detail in each function's comment section below.
	 */
	// table name
	private $tbl_blog = 'blog';
	private $tbl_user = 'masteruser';
	private $tbl_blog_category= 'blog_category';

	// get number of users in database
	function count_all($cond=NULL){
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		return $this->db->count_all($this->tbl_blog);
	}
	//Gets all entries in the blog table
    public function get_all_entries($cond=NULL, $sort=NULL, $limit=10, $offset=0)
    {	
	
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		//sorting
		if ($sort != NULL){
			for ($i = 0; $i < count($sort); $i++)
				$this->db->order_by($sort[$i]['column'], $sort[$i]['order']);
		}
		//limit & offset
		$this->db->limit($limit, $offset);
        $this->db->select('summary, title,creation_date, url_title, authorID, blog_category.description, masteruser.firstName AS author'); 
		$this->db->from($this->tbl_blog);
		$this->db->join($this->tbl_user, 'masteruser.userID = blog.authorID', 'left');
		$this->db->join($this->tbl_blog_category, 'blog.categoryID = blog_category.blogcategory_id', 'left');
        $query = $this->db->get();
        
        if ($query->num_rows() > 0)
        {
          return $query->result();
        } else {
          return array();
        }

    }
    // get archive posts
	public function get_archive()
	{
		$sql = "SELECT DATE_FORMAT(creation_date,'%Y') AS YEAR, DATE_FORMAT(creation_date,'%M') AS MONTH, COUNT(*) AS TOTAL FROM blog GROUP BY DATE_FORMAT(creation_date,'%Y') , DATE_FORMAT(creation_date,'%M') ORDER BY YEAR DESC";
		$query = $this->db->query($sql);
		$data = array();

		foreach ($query->result() as $row)
		{
			$year = $row->YEAR;

			$data[$year][] = $row;
		}
		$query->free_result();
		return $data;
	}
    //gets a single entry based on its url title
    public function get_entry($url_title)
    {
      $this->db->select('title, entry, authorID,masteruser.firstName as author, creation_date');
      $this->db->from($this->tbl_blog);
	  $this->db->join($this->tbl_user, 'masteruser.userID = blog.authorID', 'left');
      $this->db->where('url_title', $url_title);
	  $query = $this->db->get();
	  
      if ($query->num_rows() == 1)
      {
        return $query->row();
      } else {
        return false; 
      }
    }
	//save article
	public function save($article)
	{
		$this->db->insert($this->tbl_blog, $article);
		return $this->db->insert_id();
	}
	//get category	
	public function get_category($cond = NULL){
		if ($cond != NULL){
			foreach ($cond as  $param => $item)
				$this->db->where($param , $item);
		}
		
		$query = $this->db->get($this->tbl_blog_category);
		$result = array();
		$result[''] = 'Select Category';
		foreach ($query->result() as $row){
			$result[$row->blogcategory_id]= $row->description;
		}
		$query->free_result(); //free result
		return $result;
	}

}
/* End of file blog_model.php */
/* Location: ./models/blog_model.php */ 
?>